package com.example.drainage.ui.multipletype

import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.arm.base.BaseActivity
import com.example.arm.ext.dpInt
import com.example.drainage.R
import com.example.drainage.databinding.ActivityMultipleTypeRecyclerviewBinding
import timber.log.Timber
import java.util.*

/**
 * @author 创建人 ：yanghaozhang
 * @version 1.0
 * @package 包名 ：com.example.armmvvm.ui.recyclerview.mutipletype
 * @createTime 创建时间 ：2021/11/26
 * @modifyBy 修改人 ：
 * @modifyTime 修改时间 ：
 * @modifyMemo 修改备注：
 */
class MultipleTypeRecyclerViewActivity : BaseActivity<ActivityMultipleTypeRecyclerviewBinding>() {
    override fun initView(savedInstanceState: Bundle?): ActivityMultipleTypeRecyclerviewBinding =
        ActivityMultipleTypeRecyclerviewBinding.inflate(layoutInflater)

    override fun initData(savedInstanceState: Bundle?) {
        val multipleTypeAdapter = MultipleTypeAdapter()
        val list = listOf(
            MultipleTypeBean.MultipleTypeBeanA("A"),
            MultipleTypeBean.MultipleTypeBeanB("a00"),
            MultipleTypeBean.MultipleTypeBeanB("a001"),
            MultipleTypeBean.MultipleTypeBeanB("a002"),
            MultipleTypeBean.MultipleTypeBeanB("a003"),
            MultipleTypeBean.MultipleTypeBeanB("a004"),
            MultipleTypeBean.MultipleTypeBeanA("B"),
            MultipleTypeBean.MultipleTypeBeanB("b00"),
            MultipleTypeBean.MultipleTypeBeanB("b001"),
            MultipleTypeBean.MultipleTypeBeanB("b002"),
            MultipleTypeBean.MultipleTypeBeanB("b003"),
            MultipleTypeBean.MultipleTypeBeanB("b004"),
            MultipleTypeBean.MultipleTypeBeanA("C"),
            MultipleTypeBean.MultipleTypeBeanB("C00"),
            MultipleTypeBean.MultipleTypeBeanB("C001"),
            MultipleTypeBean.MultipleTypeBeanB("C002"),
            MultipleTypeBean.MultipleTypeBeanB("C003"),
            MultipleTypeBean.MultipleTypeBeanB("C004"),
            MultipleTypeBean.MultipleTypeBeanB("C004"),
            MultipleTypeBean.MultipleTypeBeanB("C004"),
            MultipleTypeBean.MultipleTypeBeanB("C004"),
            MultipleTypeBean.MultipleTypeBeanB("C004"),
            MultipleTypeBean.MultipleTypeBeanB("C004"),
            MultipleTypeBean.MultipleTypeBeanA("D"),
            MultipleTypeBean.MultipleTypeBeanB("d00"),
            MultipleTypeBean.MultipleTypeBeanB("d001"),
            MultipleTypeBean.MultipleTypeBeanB("d002"),
            MultipleTypeBean.MultipleTypeBeanB("d003"),
            MultipleTypeBean.MultipleTypeBeanB("d004"),
            MultipleTypeBean.MultipleTypeBeanB("d004"),
            MultipleTypeBean.MultipleTypeBeanB("d004"),
            MultipleTypeBean.MultipleTypeBeanB("d004"),
            MultipleTypeBean.MultipleTypeBeanB("d004"),
            MultipleTypeBean.MultipleTypeBeanB("d004"),
            MultipleTypeBean.MultipleTypeBeanA("E"),
            MultipleTypeBean.MultipleTypeBeanB("e00"),
            MultipleTypeBean.MultipleTypeBeanB("e001"),
            MultipleTypeBean.MultipleTypeBeanB("e002"),
            MultipleTypeBean.MultipleTypeBeanB("e003"),
            MultipleTypeBean.MultipleTypeBeanB("e004"),
            MultipleTypeBean.MultipleTypeBeanB("e004"),
            MultipleTypeBean.MultipleTypeBeanB("e004"),
            MultipleTypeBean.MultipleTypeBeanB("e004"),
            MultipleTypeBean.MultipleTypeBeanB("e004"),
            MultipleTypeBean.MultipleTypeBeanB("e004"),
        )
        multipleTypeAdapter.updateList(list)
        binding.recyclerView.adapter = multipleTypeAdapter
        binding.recyclerView.addItemDecoration(
            MultipleTypeDecoration(binding.recyclerView,
                object : MultipleTypeDecoration.MultipleTypeDecorationCallback {
                    override fun getDecorationFlag(position: Int): String {
                        val item = list[position]
                        return when (item) {
                            is MultipleTypeBean.MultipleTypeBeanA -> item.title
                            is MultipleTypeBean.MultipleTypeBeanB -> item.itemName
                        }.substring(0, 1).toUpperCase(Locale.ROOT)
                    }

                    override fun getDecorationViewType(position: Int, flag: String): Int {
                        return when (flag) {
                            "A", "C" -> {
                                0
                            }
                            else -> {
                                1
                            }
                        }
                    }

                    override fun getDecorationViewByType(recyclerView: RecyclerView, type: Int): View {
                        return if (type == 0) {
                            layoutInflater.inflate(R.layout.item_multiple_type_decoration_a, recyclerView, false)
                        } else {
                            layoutInflater.inflate(R.layout.item_multiple_type_decoration_b, recyclerView, false)
                        }
                    }

                    override fun onBindView(decorationBean: MultipleTypeDecoration.MultipleTypeDecorationBean, position: Int) {
                        Timber.d("--${decorationBean.flag}--")
                        if (decorationBean.type == 0) {
                            decorationBean.view.findViewById<TextView>(R.id.tv_left).text = "--${decorationBean.flag}--"
                        } else {
                            decorationBean.view.findViewById<TextView>(R.id.tv_top).text = "--${decorationBean.flag}--"
                        }
                    }

                })
        )
    }


    sealed class MultipleTypeBean {
        abstract val type: Int

        companion object {
            const val TYPE_A = 0
            const val TYPE_B = 1
        }

        class MultipleTypeBeanA(val title: String) : MultipleTypeBean() {
            override val type: Int
                get() = TYPE_A
        }

        class MultipleTypeBeanB(val itemName: String) : MultipleTypeBean() {
            override val type: Int
                get() = TYPE_B
        }
    }

    open class MultipleTypeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    class MultipleTypeViewHolderA(itemView: View) : MultipleTypeViewHolder(itemView)
    class MultipleTypeViewHolderB(itemView: View) : MultipleTypeViewHolder(itemView)

    class MultipleTypeAdapter : RecyclerView.Adapter<MultipleTypeViewHolder>() {

        private val dataList = mutableListOf<MultipleTypeBean>()

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MultipleTypeViewHolder {
            val textView = TextView(parent.context)
            textView.setPadding(0, 30, 0, 30)
            textView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 50f.dpInt)
            textView.gravity = Gravity.CENTER
            return when (viewType) {
                MultipleTypeBean.TYPE_A -> {
                    textView.setBackgroundColor(Color.YELLOW)
                    MultipleTypeViewHolderA(textView)
                }
                MultipleTypeBean.TYPE_B -> {
                    textView.setBackgroundColor(Color.WHITE)
                    MultipleTypeViewHolderB(textView)
                }
                else -> {
                    MultipleTypeViewHolder(textView)
                }
            }
        }

        override fun onBindViewHolder(holder: MultipleTypeViewHolder, position: Int) {
            when (holder) {
                is MultipleTypeViewHolderA -> {
                    val multipleTypeBeanA = dataList[position] as MultipleTypeBean.MultipleTypeBeanA
                    (holder.itemView as TextView).text = multipleTypeBeanA.title
                }
                is MultipleTypeViewHolderB -> {
                    val multipleTypeBeanB = dataList[position] as MultipleTypeBean.MultipleTypeBeanB
                    (holder.itemView as TextView).text = multipleTypeBeanB.itemName
                }
            }
        }

        override fun getItemCount(): Int {
            return dataList.size
        }

        override fun getItemViewType(position: Int): Int {
            return dataList.getOrNull(position)?.type ?: 0
        }

        fun updateList(list: List<MultipleTypeBean>?) {
            dataList.clear()
            list ?: return
            dataList.addAll(list)
        }

    }
}